package com.yinhe.music.yhmusic.personal;

import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.TextView;

import com.yinhe.music.common.GlideHelper.GlideHelper;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.adapter.UserInfoAdapter;
import com.yinhe.music.yhmusic.base.BaseBarStateChangeListener;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceActivity;
import com.yinhe.music.yhmusic.constants.KeyConstants;
import com.yinhe.music.yhmusic.model.UserInfo;
import com.yinhe.music.yhmusic.utils.ViewUtils;

import butterknife.BindView;

/**
 * 歌手详细页
 */

public class UserInfoActivity extends BaseServiceActivity implements UserInfoContract.IUserInfoView {
    @BindView(R.id.backdrop)
    ImageView mBackdrop;
    @BindView(R.id.transparent_toolbar)
    Toolbar toolbar;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.tabs)
    TabLayout tabLayout;
    @BindView(R.id.transparent_toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.user_head)
    ImageView userImage;
    @BindView(R.id.tv_user_name)
    TextView userName;
    private int userId;
    private ActionBar actionBar;
    private UserInfoPresenter mPresenter;

    @Override
    public int getLayoutResId() {
        return R.layout.activity_user_page;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new UserInfoPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    public void initView() {
        if (getIntent() != null) {
            userId = getIntent().getIntExtra(KeyConstants.KEY_USER_ID, 0);
        }

        hideToolbar();
        setupToolbar();
        showQuickControl(true);
        mPresenter.getUserInfo(String.valueOf(userId));
    }

    /**
     * 设置标题栏
     */
    private void setupToolbar() {
        setSupportActionBar(toolbar);
        toolbar.setTitle("");
        actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);
            actionBar.setTitle("");
        }
    }

    /**
     * 初始化ViewPager
     */
    private void setViewPager(String intro) {
        tabLayout.setupWithViewPager(viewpager);
        UserInfoAdapter mAdapter = new UserInfoAdapter(getSupportFragmentManager(), intro, String.valueOf(userId));
        viewpager.setAdapter(mAdapter);
        ViewUtils.reflexIndicator(tabLayout, 40);
    }

    @Override
    public void setUserInfoUI(UserInfo userInfo) {
        GlideHelper.setCircleImageResource(userImage, userInfo.getImage(), R.drawable.default_user_head);
        userName.setText(userInfo.getNickname());
        setViewPager(userInfo.getIntro());

        appbar.addOnOffsetChangedListener(new BaseBarStateChangeListener() {
            @Override
            public void onOffSetChanged(AppBarLayout appBarLayout, int i) {
                actionBar.setBackgroundDrawable(null);
                float a = (float) Math.abs(i) / (appBarLayout.getTotalScrollRange());
                userImage.setAlpha(1f - a);
                userName.setAlpha(1f - a);
            }

            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                if (state == State.EXPANDED) {
                    //展开状态
                    toolbarTitle.setText("");

                } else if (state == State.COLLAPSED) {
                    //折叠状态
                    toolbarTitle.setText(userInfo.getNickname());
                }
            }
        });
    }

}
